package com.aaa.reflect;

import java.lang.reflect.Field;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/18 9:51
 * @description：
 * @modified By：
 * @version:
 */
public class FieldTest {
    public static void main(String[] args) throws Exception {
        Class<Person> clazz = Person.class;
        //获取所有的公有属性
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("============");
        //获取所有的属性，包含私有
        Field[] fields2 = clazz.getDeclaredFields();
        for (Field field : fields2) {
            System.out.println(field);
        }
        //按照属性名获取属性
        Field id1 = clazz.getField("id");
        Field id2 = clazz.getField("id");
        boolean ret = id1.equals(id2);
        System.out.println(ret);
        //get(Object obj)	获得obj中对应的属性值
        Person person = new Person(1000,"张三",38);
        Object o = id1.get(person);
        System.out.println(o);
        System.out.println("修改之前"+person.toString());
        id1.set(person,2000);
        Field name = clazz.getDeclaredField("name");
        //临时获取访问私有属性的权限
        name.setAccessible(true);
        name.set(person,"张三丰");
        System.out.println("修改之后"+person.toString());


    }
}
